// Learn JavaScript:
//  - Created by zhanghejun
// 	- creation time ：  2021-08-31
//  - 
let LayerManager = module.exports = {};

function arrRemove(array,element){
    var arr = [];
    for (let index = 0; index < array.length; index++) {
        if (array[index] != element){
            arr.push(array[index]);
        }
    }
    return arr;
};

LayerManager.init = function () {
    this.layerArr = [];
};

// 新建Layer
LayerManager.create = function(path,className,param = null,zroder=ZORDER.LAYER){
    let self = this;
    cc.resources.load(path,cc.Prefab, function(err, loadedResource) {
        if (err) {cc.log('载入预制资源失败， 原因:' + err); return}

        let tipPrefab = cc.instantiate(loadedResource)
        let tipScript = tipPrefab.getComponent(className);
        tipScript.init && tipScript.init(param);

        self.layerArr.push(className);
        cc.director.getScene().addChild(tipPrefab, zroder,className)
    });
};

// 移除Layer
LayerManager.remove = function(className){
    let self = this;
    let node = cc.director.getScene().getChildByName(className);
    if (node !== null) {
        
        self.layerArr = arrRemove(self.layerArr,className);
        node.stopAllActions()
        node.destroy()
    }
};

// 移除全部Layer
LayerManager.removeAll = function(){
    let self = this;

    for (let index = 0; index < self.layerArr.length; index++) {
        let node = cc.director.getScene().getChildByName(self.layerArr[index]);
        if (node !== null) {
            
            node.stopAllActions()
            node.destroy()
        }
    }
    self.layerArr = [];
};
